package com.lottery.bootdo.common.controller.system;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lottery.bootdo.common.service.ActivityInfoService;
import com.lottery.bootdo.common.utils.PageUtils;
import com.lottery.bootdo.common.utils.StringUtils;
import com.lottery.common.contants.ReQueryResult;
import com.lottery.common.entity.*;
import com.lottery.common.service.DecryptToObjectService;
import com.lottery.common.service.FeedbackInfoService;
import com.lottery.common.service.MessageInfoService;
import com.lottery.common.service.TextInfoService;
import com.lottery.common.thread.SendPushTool;
import com.lottery.common.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static com.lottery.common.contants.Constants.*;

/**
 * @author o551wwl
 */
@Controller
@RequestMapping("/message")
@Slf4j
@Api(value = "/message",tags="消息操作类")
public class MessageController {
	private ReQueryResult result = new ReQueryResult();

	@Autowired
	private MessageInfoService messageInfoService;
	@Autowired
	private FeedbackInfoService feedbackInfoService;
	@Autowired
	private ActivityInfoService activityService;
	@Autowired
	private TextInfoService textInfoService;
	@Autowired
	private DecryptToObjectService decryptToObjectService;

	@ApiOperation(value = "消息列表")
	@RequiresPermissions("message:list")
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	String index() {
		return "common/message/index";
	}

	@ApiOperation(value = "推送详情")
	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
	@RequiresPermissions("message:detail")
	String detail(@PathVariable() Long id, Model model) {
		ActivityInfo info=activityService.getById(id);
		model.addAttribute("info", info);
		return "common/message/detail";
	}

	@ApiOperation(value = "添加消息")
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	@RequiresPermissions("message:edit")
	String edit( @PathVariable() Long id, Model model) {
		ActivityInfo info=activityService.getById(id);
		if (info==null){
			info=new ActivityInfo();
		}
		model.addAttribute("info", info);
		return "common/message/edit";
	}



	@ApiOperation(value = "意见反馈列表")
	@RequiresPermissions("feedback:list")
	@RequestMapping(value = "/feedback/index", method = RequestMethod.GET)
	String feedback() {
		return "common/feedback/index";
	}


	@ApiOperation(value = "消息列表")
	@RequestMapping(value = "", method = RequestMethod.GET)
	@RequiresPermissions("message:list")
	@ResponseBody
	public PageUtils messageAll(String json){
		log.info("__________ messageAll");
		result = new ReQueryResult();
		NutMap nutMap=decryptToObjectService.jsonToObject(json,NutMap.class);
		log.info("nutmap is {}", JSON.toJSONString(nutMap));

		QueryWrapper<MessageInfo> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("is_del",1);
		Page<MessageInfo> page = new Page<>(nutMap.getInt("pageIndex"),nutMap.getInt("pageLength"));

		IPage<MessageInfo> records=messageInfoService.page(page,queryWrapper);
		log.info(JSON.toJSONString(records));
		PageUtils pageUtils = new PageUtils(records.getRecords(),new BigDecimal(records.getTotal()).intValue());
		return pageUtils;
	}

	@ApiOperation(value = "添加消息")
	@RequestMapping(value = "", method = RequestMethod.POST)
	@RequiresPermissions("message:edit")
	@ResponseBody
	ReQueryResult saveOrUpdate( MessageInfo info) {
		log.info("__________ update");
		result = new ReQueryResult();
		messageInfoService.saveOrUpdate(info);
		result.success(SUCCESS_DESC);
		return result;
	}

	@ApiOperation(value = "消息删除")
	@RequestMapping(value = "", method = RequestMethod.DELETE)
	@RequiresPermissions("message:del")
	@ResponseBody
	ReQueryResult messageDel( MessageInfo info) {
		log.info("__________ messageDel");
		result = new ReQueryResult();
		messageInfoService.updateById(info);
		result.success(SUCCESS_DESC);
		return result;
	}

	@RequestMapping(value = "/batchDel", method = RequestMethod.DELETE)
	@RequiresPermissions(value={"message:del"},logical= Logical.OR)
	@ResponseBody
	public ReQueryResult batchDelete(@RequestParam("ids[]")Long[] ids){
		log.info("__________batchDel");
		result = new ReQueryResult();
		List<MessageInfo> list=new ArrayList<>();
		MessageInfo info;
		for (Long id:ids){
			info=new MessageInfo();
			info.setId(id);
			info.setIsDel(TYPE_ZERO);
			list.add(info);
		}
		messageInfoService.updateBatchById(list);
		result.success(SUCCESS_DESC);
		return result;
	}

	@ApiOperation(value = "处理意见反馈")
	@RequestMapping(value = "/feedback", method = RequestMethod.POST)
	@RequiresPermissions("feedback:edit")
	@ResponseBody
	ReQueryResult feedbackEdit(FeedbackInfo info,String content,String remark) {
		log.info("__________ feedbackEdit");
		result = new ReQueryResult();
		info.setStatus(TYPE_ZERO);
		feedbackInfoService.saveOrUpdate(info);
		MessageInfo messageInfo=new MessageInfo(info.getMemberId(),content,TYPE_ONE,remark,TYPE_ONE,info.getId().toString());
		messageInfoService.save(messageInfo);
		NutMap nutMap=new NutMap();
		nutMap.setv("message",messageInfo.getMessage()).setv("memberId",messageInfo.getMemberId());
		SendPushTool.getInstance().addSend(nutMap);
		result.success(SUCCESS_DESC);
		return result;
	}
}
